package com.dong.handler;


import com.dong.response.ResponseResult;
import com.dong.response.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description 统一异常处理
 * @Author dongjian 2021/1/20 17:36
 **/
@ControllerAdvice
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {


    /**
     * 全局异常处理
     **/
    @ExceptionHandler(Exception.class)
    public ResponseResult error(Exception e) {
        e.printStackTrace();
        return ResponseResult.error();
    }

    /**
     * @return
     * @Description 算术异常
     **/
    @ExceptionHandler(ArithmeticException.class)
    public ResponseResult error(ArithmeticException e) {
        e.printStackTrace();
        return ResponseResult.error().code(ResultCodeEnum.ARITHMETIC_EXCEPTION.getCode())
                .message(ResultCodeEnum.ARITHMETIC_EXCEPTION.getMessage());
    }

    /**
     * <p>
     * 业务异常处理
     * </p>
     *
     * @param e
     * @return
     */
    @ExceptionHandler(BusinessException.class)
    public ResponseResult error(BusinessException e) {
        log.error(e.getErrMsg());
        return ResponseResult.error()
                .code(e.getCode())
                .message(e.getErrMsg());
    }
}
